package microservices.spring_cloud_server.rest;

import java.util.List;

import javax.ws.rs.PathParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import microservices.spring_cloud_server.annontation.Permission;
import microservices.spring_cloud_server.dao.MenuDao;
import microservices.spring_cloud_server.vo.MenuVO;
import microservices.spring_cloud_server.vo.PageVO;
import microservices.spring_cloud_server.vo.PagedResult;

@RestController
public class MenuService {
	@Autowired
	MenuDao menuDao;

	@RequestMapping(value = "/menuquery/{pageSize}/{pageNo}", method = RequestMethod.POST)
	@Permission(requestPermission = "MenuService.read")
	public PagedResult<MenuVO> queryMenuPagedList(@RequestBody MenuVO menuVO,
			@PathParam(value = "pageVO") PageVO pageVO) throws InterruptedException {
		return menuDao.queryMenuPagedList(menuVO, pageVO);
	}

	@RequestMapping(value = "/menuadd", method = RequestMethod.POST)
	public void insertMenu(@RequestBody MenuVO menuVO) {
		menuDao.addMenu(menuVO);
	}

	@RequestMapping(value = "/menuupdate", method = RequestMethod.POST)
	public void updateMenu(@RequestBody MenuVO menuVO) {
		menuDao.updateMenu(menuVO);
	}

	@RequestMapping(value = "/getmenu", method = RequestMethod.POST)
	public List<MenuVO> getMenu() {
		List<MenuVO> parentMenuList = menuDao.queryParentMenuList();
		for (MenuVO menuVO : parentMenuList) {
			getFullMenu(menuVO);
		}
		return parentMenuList;
	}

	public void getFullMenu(MenuVO menuVO) {
		MenuVO qureyVO = new MenuVO();
		qureyVO.setParentMenuId(menuVO.getMenuId());
		List<MenuVO> childMenu = menuDao.queryMenuList(qureyVO, new PageVO()).getResultList();
		if (childMenu != null && childMenu.size() > 0) {
			for (MenuVO childMenuVO : childMenu) {
				this.getFullMenu(childMenuVO);
			}
		}
		menuVO.setChildren(childMenu);
	}

	@RequestMapping(value = "/getmenuroot", method = RequestMethod.POST)
	public List<MenuVO> getMenuRoot() {
		List<MenuVO> parentMenuList = menuDao.queryParentMenuList();
		for (MenuVO menuVO : parentMenuList) {
			getFullMenuRoot(menuVO);
		}
		return parentMenuList;
	}

	public void getFullMenuRoot(MenuVO menuVO) {
		MenuVO qureyVO = new MenuVO();
		qureyVO.setParentMenuId(menuVO.getMenuId());
		List<MenuVO> childMenu = menuDao.queryMenuRootList(qureyVO);
		if (childMenu != null && childMenu.size() > 0) {
			for (MenuVO childMenuVO : childMenu) {
				this.getFullMenuRoot(childMenuVO);
			}
		}
		menuVO.setChildren(childMenu);
	}

	@RequestMapping(value = "/getrouters", method = RequestMethod.POST)
	public List<MenuVO> getRouters() {
		return menuDao.queryRouters();
	}

	@RequestMapping(value = "/menudelete", method = RequestMethod.POST)
	public void batchDeleteRole(@RequestBody List<MenuVO> menuList) {
		menuDao.batchDeleteMenu(menuList);
	}
}
